<?php

namespace app\index\controller;

use app\data\model\ActActivity;
use app\data\model\ActActivityTicket;
use app\data\model\ActOrder;
use app\data\model\ActTicketDetails;
use app\data\model\ActTicketGroup;
use app\index\service\LoginUserService;
use think\facade\Db;

class ActivityTicket extends BaseController
{
    protected function initialize()
    {
        parent::initialize();
        $this->needLogin();
    }

    /**
     * @name:票券购买
     * @author:Sam
     * @date:2024-04-14
     * @param:[string] param [desc]
    */
    public function buy()
    {
        $post_data = request()->post();
        $this->validate($post_data,[
            'activity_id'                =>'require',
            'ticket_id'                  =>'require',
            'ticket_type'                =>'require',
            'ticket_form'                =>'require',
        ],[
            'activity_id.require'      =>'活动不得为空',
            'ticket_id.require'        =>'票券不得为空',
            'ticket_type.require'      =>'票券类型不得为空',
            'ticket_form.require'      =>'报名表不得为空',
        ]);
        Db::transaction(function () use ($post_data) {
            $activity = ActActivity::where('id',$post_data['activity_id']) ->find();
            if(!$activity) return $this->error('活动不存在');
            if(!in_array($post_data['ticket_type'],[1,2]))  return $this->error('数据不正确');
            if($post_data['ticket_type'] == 1){
                //普通
                $ticket = ActTicketDetails::where('id', $post_data['ticket_id'])->where('activity_id',$post_data['activity_id'])->lock(true)->find();
                $stock  = $ticket->rest_quantity;
                 //已购数量
                $buy_number = ActOrder::where('buy_user_id',LoginUserService::get('id'))->where('status',4)->where('activity_id',$post_data['activity_id'])->where('ticket_id',$ticket->id)->count();
                if($buy_number >= $ticket->ticketModel->ticket_max)  $this->error('超过购买限制'.$ticket->ticket_max.'张');
            }else{
                //分组票
                $ticket = ActTicketGroup::where('id', $post_data['ticket_id'])->where('activity_id',$post_data['activity_id'])->lock(true)->find();
                $stock  = $ticket->group_remain_number;
                $buy_number = ActOrder::where('buy_user_id',LoginUserService::get('id'))->where('status',4)->where('activity_id',$post_data['activity_id'])->where('group_id',$ticket->id)->count();
                if($buy_number >= $ticket->ticket_max)  $this->error('超过购买限制'.$ticket->ticket_max.'张');
            }
           
            if (empty($ticket)) {
                $this->error('门票不存在！');
            }
            if (!$ticket->ifOnSale()) {
                $this->error('门票未在售！');
            }
            if($stock <=0 )   $this->error('库存不足');
            //售票
            $ticket->saleOneTicket();
            $diy_form = $post_data['ticket_form'];
            if(empty($diy_form['name']))  $this->error('报名人不得为空');
            if(empty($diy_form['email']))  $this->error('报名人邮箱不得为空');
            if(empty($diy_form['phone']))  $this->error('报名人联系方式不得为空');
            //创建订单
            ActOrder::create([
                'activity_id' => $ticket->activity_id,
                'ticket_id'   => $post_data['ticket_type'] == 1 ? $ticket->id : 0,
                'group_id'    => $post_data['ticket_type'] == 2 ? $ticket->id : 0,
                'buy_user_id' => LoginUserService::get('id'),
                'price'       => $post_data['ticket_type'] == 1 ?  $ticket->price : $ticket->group_price,
                'pay_type'    => 0,
                'status'      => 3,
                'name'        => $diy_form['name'],
                'phone'       => $diy_form['phone'],
                'email'       => $diy_form['email'],
                'ticket_name' => $post_data['ticket_type'] == 1 ? $ticket->name  :  $ticket->group_name,
                'diys'        => json_encode($diy_form),
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => 0,
                'add_time'    => time()
            ]);
        });

        $this->success('购买成功');

    }



}